package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.domain.Account;
import com.example.domain.Admin;
import com.example.domain.Department;
import com.example.exception.CustomException;
import com.example.mapper.DepartmentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.List;

@Service
public class DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    //    查全部数据
    public List<Department> selectAll(Department department) {
        return departmentMapper.selectAll(department);
    }

    //    查单个数据
    public Department selectByPrimaryKey(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    //    分页查询
    public PageInfo<Department> selectPage(Department department, Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Department> list = departmentMapper.selectAll(department);
        return PageInfo.of(list);
    }

//    // 新增数据---注册
//    public void add(Department department) {
//        String username = department.getUsername();
//        Department dbDepartment = departmentMapper.selectByUsername(username);
//        if (dbDepartment != null){    //账号存在，无法注册
//            throw new CustomException("500","账号存在，无法注册");
//        }
//        if (StrUtil.isBlank(department.getPassword())){   //密码没填
//            department.setPassword("12345678");    //默认密码123
//        }
////        if (StrUtil.isBlank(department.getName())){   //名字没填
////            department.setPassword("张三");    //默认名字
////        }
//        department.setRole("EMP");    //设置角色
//
//        departmentMapper.insert(department);
//    }

    // 更新数据
    public void update(Department department) {

        departmentMapper.updateByPrimaryKey(department);

    }

    //根据id  删除数据
    public void deleteById(Integer id) {
        departmentMapper.deleteByPrimaryKey(id);
    }


    //  iter      批量删除---循环调用单个id删除
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }


}
